package com.fuze.energy.config;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author sepc_
 * @date 6/11/2021 15:01
 * 自定义线程池，目的是不希望使用 Spring 提供的线程池
 */
@Configuration
public class ThreadPoolExecutorConfig {

    @Bean
    public ThreadPoolExecutor taskExecutor() {
        return new ThreadPoolExecutor(
                5,
                10,
                10,
                TimeUnit.MINUTES,
                new LinkedBlockingDeque<>(1024),
                new ThreadFactoryBuilder()
                        .setNameFormat("task-thread-executor-%d")
                        .build(),
                new ThreadPoolExecutor.AbortPolicy()
        );
    }

}
